<?php


namespace app\common\cachemodel;


use app\common\model\BaseCache;
use think\Env;
use XznsTools\cache\CacheManage;

class ZlcrSlide extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zlcr_slide';

    protected $cacheModel = 'astr_admin.slide';

    /**
     * 获取轮播信息
     * @param bool $is_get_content 是否获取详情内容
     * @return array
     * @throws \Exception 删除缓存方法   CacheManage::getInstance()->setCacheConfig($this->cacheModel)->rmCache();
     */
    public function getSlideList($is_get_content = true): array
    {
        $data = CacheManage::getInstance()->setCacheConfig($this->cacheModel)->getDataAndSet(function ()
        {
            try
            {
                $res = $this->where('status', 1)->order('sort desc')->field('id,sort,name,description,content,link,target,image')->select();

                return json_encode($res, JSON_UNESCAPED_UNICODE);
            }
            catch (\Exception $e)
            {
                return '[]';
            }
        });

        $dataArray = json_decode($data, true);

        $resData = [];

        $host = Env::get('host.photo');

        foreach ($dataArray as $item)
        {
            $resData[] = [
                'id' => (int)$item['id'],
                'img' => $host . $item['image'],
                'target' => $item['target'],
                'link' => $item['link'],
                'content' => $is_get_content ? $item['content'] : '',
                'name' => $item['name'],
            ];
        }

        return $resData;
    }
}
